<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategorySelect
        @getCategoryId="getCategoryId"
        :show="scene != 0"
      ></CategorySelect>
    </el-card>
    <el-card>
      <!-- 这里有三部分进行切换 -->
      <div v-show="scene == 0">
        <!-- 这里展示spu列表的结构 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!Id3"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <!-- 这里的按钮将来用hintButton替换 -->
              <hint-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></hint-button>
              <hint-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              ></hint-button>
              <hint-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部sku列表"
                @click="handler(row)"
              ></hint-button>

              <el-popconfirm title="确定删除吗？" @onConfirm="deleteSpu(row)">
                <hint-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  slot="reference"
                >
                </hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="margin-top: 20px; text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 8]"
          :page-size="limit"
          layout="prev,pager,next,jumper,->,sizes,total"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <SkuForm
        v-show="scene == 2"
        ref="sku"
        @changeScenes="changeScenes"
      ></SkuForm>
    </el-card>
    <!-- before-close	关闭前的回调，会暂停 Dialog 的关闭	function(done)，done 用于关闭 Dialog -->
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table :data="skuList" border style="width: 100%" v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width"></el-table-column>
        <el-table-column prop="price" label="价格" width="width"></el-table-column>
        <el-table-column prop="weight" label="重量" width="width"></el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row}">
            <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px;">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "@/views/product/Spu/SpuForm";
import SkuForm from "@/views/product/Spu/SkuForm";
export default {
  name: "Spu",
  components: {
    SpuForm,
    SkuForm,
  },
  data() {
    return {
      Id1: "",
      Id2: "",
      Id3: "",
      page: 1, //分页器当前是第几页
      limit: 3, //每一页需要展示多少条数据
      total: 0, //存储数据条数
      records: [], //存储spu列表数据
      scene: 0, //0 1 2 分别展示不同的部分
      dialogTableVisible: false, //控制对话框的显示和隐藏
      spu: {},
      skuList: [], //存储sku列表数据
      loading:true,
    };
  },
  methods: {
    // 三级联动的自定义事件的回调
    getCategoryId({ categoryId, level }) {
      console.log(categoryId, level, "spu id和level");
      if (level == 1) {
        this.Id1 = categoryId;
        this.Id2 = "";
        this.Id3 = "";
      } else if (level == 2) {
        this.Id2 = categoryId;
        this.Id3 = "";
      } else {
        // 代表三级分类的id有了，可以发送请求获取品牌属性
        this.Id3 = categoryId;
        // 获取spu列表数据进行展示
        this.getSpuList();
      }
    },
    // 获取spu列表数据的方法
    async getSpuList() {
      const { page, limit, Id3 } = this;
      // 携带三个参数：page limit Id3
      let result;
      try {
        result = await this.$API.spu.reqSpuList(page, limit, Id3);
        console.log(result, "获取spu列表数据成功");
      } catch (error) {
        result = error;
        console.log(result, "获取spu列表数据失败");
      }
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    // 点击第几页就会触发的回调
    handleCurrentChange(page) {
      console.log(page, "page");
      this.page = page;
      this.getSpuList();
    },
    // 某一页展示数据条数发生变化就会触发
    handleSizeChange(limit) {
      console.log(limit, "limit");
      this.limit = limit;
      this.getSpuList();
    },
    // 添加SPU按钮的回调
    addSpu() {
      this.scene = 1;
      // 通知spuform发请求---两个
      this.$refs.spu.addSpuData(this.Id3);
    },
    // 修改某一个SPU按钮的回调
    updateSpu(row) {
      this.scene = 1;
      // 通知spuform发请求---四个
      this.$refs.spu.initSpuData(row);
    },
    // 自定义事件的回调
    changeScene({ scene, flag }) {
      // 切换场景（结构）
      this.scene = scene;
      // 子组件通知父组件切换场景后需要再次获取SPU列表的数据进行展示，并停留在当前页
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList();
      }
    },
    // 删除spu按钮的回调
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        this.$message({
          type: "success",
          message: "删除成功",
        });
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    // 添加sku按钮的回调
    addSku(row) {
      this.scene = 2;
      // 父组件调用子组件的方法，让子组件发请求---三个
      this.$refs.sku.getData(this.Id1, this.Id2, row);
    },
    // skuForm通知父组件修改场景
    changeScenes(scene) {
      this.scene = scene;
    },
    // 查看sku按钮的回调
    async handler(spu) {
      this.dialogTableVisible = true;
      this.spu = spu; //保存信息
      // 发请求——获取sku列表的数据进行展示
      let result = await this.$API.spu.reqSkuList(spu.id);
      console.log(result, "获取sku列表的数据");
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading=false;//loading隐藏
      }
    },
    // 关闭对话框的回调
    close(done) {
      this.loading=true;
      this.skuList=[];//关闭dialog之前清空数据
      done();//关闭对话框
    }
  },
};
</script>

<style>
</style>